<template>
    <div>
        <el-row>
            <el-col :span="8">
                <el-card shadow="hover" id="res_percentage_chart" class="res_percentage_chart">
                </el-card>
            </el-col>
            <el-col :span="16">
                <el-card shadow="hover" id="sample_data_chart" class="sample_data_chart">
                </el-card>
            </el-col>
        </el-row>

        <el-row>
            <el-col :span="24">
                <el-card shadow="hover" id="fraud_counts_chart" class="fraud_counts_chart">
                </el-card>
            </el-col>
        </el-row>
    </div>

</template>

<script>
import * as echarts from 'echarts';

export default {
    data() {
        return {
        }
    },
    methods: {
        renderPieChart() {
            if (this.result && this.result.res_percentage_data) {
                var myChart = echarts.init(document.getElementById('res_percentage_chart'));

                const option = {
                    title: {
                        text: '欺诈比例分析',
                        left: 'center'
                    },
                    tooltip: {
                        trigger: 'item',
                        formatter: '{a} <br/>{b} : {c} ({d}%)'
                    },
                    legend: {
                        orient: 'vertical',
                        left: 'right',
                        data: ['非欺诈', '欺诈']
                    },
                    series: [
                        {
                            name: 'RES列01比例',
                            type: 'pie',
                            radius: '55%',
                            center: ['50%', '50%'],
                            data: [
                                { value: this.result.res_percentage_data.Value[0], name: '非欺诈' },
                                { value: this.result.res_percentage_data.Value[1], name: '欺诈' }
                            ],
                            label: {
                                show: true,
                                formatter: '{b} : {c} ({d}%)'
                            }
                        }
                    ]
                };

                myChart.setOption(option);
            }
        },
        renderFraudChart() {
            if (this.result && this.result.fraud_data) {
                var myChart = echarts.init(document.getElementById('fraud_counts_chart'));

                const option = {
                    title: {
                        text: '不同病种下的欺诈情况',
                        left: 'center'
                    },
                    xAxis: {
                        type: 'category',
                        name: '病种编号',
                        data: Array.from({ length: this.result.fraud_data.fraud_counts.length }, (_, i) => i + 1)
                    },
                    yAxis: {
                        type: 'value',
                        name: '人数'
                    },
                    series: [
                        {
                            name: '欺诈人数',
                            type: 'bar',
                            data: this.result.fraud_data.fraud_counts,
                            itemStyle: { color: 'red' }
                        },
                        {
                            name: '非欺诈人数',
                            type: 'bar',
                            data: this.result.fraud_data.non_fraud_counts,
                            itemStyle: { color: 'green' }
                        }
                    ]
                };

                // 设置全局配置
                option.visualMap = {
                    max: Math.max(Math.max(...this.result.fraud_data.fraud_counts), Math.max(...this.result.fraud_data.non_fraud_counts))
                };

                myChart.setOption(option);
            }
        },
        renderSampleChart() {
            if (this.result && this.result.sample_data) {
                var myChart = echarts.init(document.getElementById('sample_data_chart'));

                const option = {
                    title: {
                        text: '费用特征分布',
                        left: 'center'
                    },
                    series: [
                        {
                            name: '费用特征占比',
                            type: 'pie',
                            radius: ['40%', '70%'],
                            data: this.result.sample_data.feature_names.map((name, index) => ({ value: this.result.sample_data.values[index], name: name })),
                            label: {
                                formatter: '{b}: {d}%',
                            },
                        }
                    ],
                    legend: {
                        orient: 'vertical',
                        left: 'right'
                    }
                };

                myChart.setOption(option);
            }
        },
    },
    computed: {
        result() {
            return this.$store.state.processedData.result;
        },
    },
    mounted() {
        this.renderPieChart();
        this.renderFraudChart();
        this.renderSampleChart();
    },
    watch: {
        result: {
            handler(newVal, oldVal) {
                if (newVal && newVal.res_percentage_data && newVal.fraud_data && newVal.sample_data) {
                    this.renderPieChart();
                    this.renderFraudChart();
                    this.renderSampleChart();
                }
            },
            deep: true,
            immediate: true
        }
    },
}
</script>

<style lang="less" scoped>
.res_percentage_chart {
    width: 53vh;
    height: 40vh;
}

.sample_data_chart {
    width: calc(107vh - 14px);
    height: 40vh;
}

.fraud_counts_chart {
    width: 160vh;
    height: 45vh;
    margin-top: 10px;
}
</style>